package site.yunnong.atvris.common.utils;

import org.apache.commons.collections.CollectionUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;

/**
 * 不想导包自己写的日期工具类
 * @author JinghaoZhang
 * @date 2021年-04月-12日
 */
public class DateUtils {

    /**
     * 时长字符串转秒数
     * @param duration
     * @return
     */
    public static long covertDurationToSecond(String duration) {
        String[] strings = duration.split(":");
        if (CollectionUtils.isEmpty(Arrays.asList(strings))) {
            throw new IllegalArgumentException("输入参数错误");
        }
        long time = 0L;
        for (int i = strings.length - 1; i > -1; i--) {
            time += Long.parseLong(strings[i]) * Math.pow(60, strings.length - i - 1);
        }
        return time;
    }

    /**
     * 秒数转时长字符串
     * @param seconds
     * @return
     */
    public static String covertSecondToDuration(Long seconds) {
        if (seconds == null || seconds < 0) {
            throw new IllegalArgumentException("输入参数错误");
        }
        int secondValue = (int)(seconds%60);
        int minuteValue = (int)(seconds/60)%60;
        int hourValue = (int)(seconds/3600);

        return (hourValue < 10 ? "0" : "") + hourValue +
                (minuteValue < 10 ? ":0" : ":") + minuteValue +
                (secondValue < 10 ? ":0" : ":") + secondValue;
    }
    
    public static LocalDateTime getDateTimeOfTimeStamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    public static void main(String[] args) {
        System.out.println(covertSecondToDuration(3602 * 24L));
    }
}
